﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand;
using RedSand.Site.Models;
using RedSand.Web.Mvc;
using System.Web.Security;

namespace RedSand.Site.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index(string keyword)
        {
            ViewBag.Title = "Title";
            ViewBag.TotalRecord = 0;
            
            ViewBag.MetaDescription = "Design website , devlopment software , convert psd to html , thiết kế website ";
            ViewBag.MetaKeywords = "Design website , build website , custom website , thiết kế website , xây dựng website";
            
            ViewBag.SearchData = new { keyword = keyword};
            return View();
        }
        [Authorize]
        public ActionResult MyList()
        {
            ViewBag.SearchData = new { UserID = UserID };
            return View("Index");
        }
        public ActionResult TopView()
        {
            ViewBag.SearchData = new { procedure = "NewsGetTopView" };
            return View("Index");
        }
        public ActionResult TopDownLoad()
        {
            ViewBag.SearchData = new { procedure = "NewsGetTopDownLoad" };
            return View("Index");
        }
        public ActionResult Contact()
        {
            return View();
        }
        public ActionResult Rules()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Login(string returnUrl)
        {
            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                AddMessage("Please login first");
            }
            return Redirect("/");
        }
        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                UserID = RsDataManager.ExecuteGetReturn("UserValidate", new { UserName = model.Username, Password = RedSand.Web.Security.RsEncryption.GetMd5Hash(model.Password) });
                if (UserID > 0)
                {
                    FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                }
                else
                {
                    AddMessage("Username or password wrong");
                }
            }
            else
            {
                AddMessage("Please enter correct all fields");
            }
            if (Request.UrlReferrer != null)
                return Redirect(Request.UrlReferrer.AbsoluteUri);
            return Redirect("/");
        }
        public ActionResult Logout()
        {
            Session.RemoveAll();
            FormsAuthentication.SignOut();
            return Redirect("/");
        }
        
    }
}
